package com.example.smart.base.adapter

import android.annotation.SuppressLint
import android.content.Context
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding

abstract class BaseRecyclerAdapter<V : ViewBinding, L>(val context: Context,
    private var l: MutableList<L> = mutableListOf()) : RecyclerView.Adapter<BaseViewHolder<V>>() {

    protected var clickListener: OnClickListener<L>? = null

    //获取数组
    fun getList() = l

    fun size(): Int = l.size

    fun isEmpty() = l.isEmpty()

    fun isNotEmpty() = l.isNotEmpty()

    open fun add(data: L, index: Int = -1) {
        if (index != -1) {
            l.add(index, data)
            notifyItemInserted(index)
            notifyItemRangeChanged(index, itemCount)
        } else {
            l.add(data)
            notifyItemInserted(size())
            notifyItemRangeChanged(size(), itemCount)
        }
    }

    open fun addAll(data: MutableList<L>, index: Int = -1) {
        if (data.isEmpty()) return
        if (data.size > 100) {
            getList().addAll(0, data)
            updateList(data)
        } else {
            data.forEach { add(it, index) }
        }
    }

    //移除一个Item
    open fun remove(position: Int, isNull: () -> Unit = {}) {
        if (position >= itemCount) return
        l.removeAt(position)
        notifyItemRemoved(position)
        // 如果移除的是最后一个，忽略
        if (position != size()) notifyItemRangeChanged(position, size() - position)
        if (isEmpty()) isNull.invoke()
    }

    //批量移除
    open fun remove(list: MutableList<Int>, isNull: () -> Unit = {}) {
        if (list.isEmpty()) return
        list.sortWith(Comparator { o1, o2 ->
            return@Comparator o1.compareTo(o2)
        })

        for (i in 0 until list.size) {
            var posi = list[i]
            posi -= i
            remove(posi)
        }
        if (isEmpty()) isNull.invoke()
    }

    //全部移除
    open fun removeAll() {
        val previousSize = size()
        l.clear()
        notifyItemRangeRemoved(0, previousSize)
    }

    //更新指定位置Item
    open fun updateItem(position: Int, data: L? = null, payload: String = "") {
        if (position >= itemCount) return
        if (data != null) l[position] = data
        if (payload.isEmpty()) notifyItemChanged(position)
        else notifyItemChanged(position, payload)
    }

    //更新数组
    @SuppressLint("NotifyDataSetChanged")
    open fun updateList(list: MutableList<L>) {
        if (list.isEmpty()) return
        l.clear()
        l.addAll(list)
        notifyDataSetChanged()
    }

    //获取一个指定位置的Item
    open fun getItemAtPosition(position: Int) = l[position]

    override fun getItemCount() = size()

    override fun onBindViewHolder(p0: BaseViewHolder<V>, p1: Int) {
    }

    //重写 解决刷新导致图片闪烁问题
    override fun onBindViewHolder(holder: BaseViewHolder<V>, position: Int, payloads: MutableList<Any>) {
        if (size() > 0 && size() == itemCount) {
            if (payloads.isEmpty()) onBindView(holder.binding, l[position], position)
            else {
                onItemChangeBindView(holder.binding, l[position], position, payloads)
            }
        }
    }

    //强制重写的加载界面方法
    abstract fun onBindView(itemView: V, bean: L, position: Int)

    //不强制重写的刷新界面用的
    open fun onItemChangeBindView(itemView: V, bean: L, position: Int, payloads: MutableList<Any>) {
    }

    fun setOnClickListener(onClickListener: OnClickListener<L>) {
        this.clickListener = onClickListener
    }

    interface OnClickListener<L> {
        fun click(bean: L, position: Int)

        fun other(bean: L, position: Int) {}

        fun third(bean: L, position: Int) {}
    }
}